<template>
  <div class="login-wrap">
    <div class="login-form">
      <el-row type="flex" justify="center" class="login-table">
        <el-col :md="20">
          <!-- 登录模块 -->
          <div>
            <div class="form-title">拼团系统</div>
            <el-form
              ref="form"
              :model="form"
              :rules="rules"
              label-width="0px"
              class="form-content"
            >
              <el-form-item prop="number" >
                <el-input
                  v-model="form.number"
                  prefix-icon="el-icon-user"
                  placeholder="请填写账号"
                  clearable
                >
                </el-input>
              </el-form-item>
              <el-form-item prop="password">
                <el-input
                  v-model="form.password"
                  prefix-icon="el-icon-lock"
                  placeholder="请输入密码"
                  clearable
                  show-password
                >
                </el-input>
              </el-form-item>

              <el-form-item prop="password2">
                <el-input
                  v-model="form.password2"
                  prefix-icon="el-icon-lock"
                  placeholder="请再次输入密码"
                  clearable
                  show-password
                >
                </el-input>
              </el-form-item>

              <el-form-item prop="userType">
                <el-radio-group
                  v-model="form.userType"
                  prefix-icon="el-icon-lock"
                >
                  <el-radio label="1">团员</el-radio>
                  <el-radio label="2">团长</el-radio>
                </el-radio-group>
              </el-form-item>

              <el-form-item
                class="text-center"
                v-loading="this.$store.state.loading"
              >
                <el-form-item>
                  <router-link
                          to="/login"
                          type="primary"
                          :underline="false"
                          style="float: right;color: white"
                  >用已有账号登录</router-link
                  >
                </el-form-item>
                <el-button
                  type="primary"
                  style="width: 100%"
                  @click="register('form')"
                  >注册</el-button
                >
              </el-form-item>
            </el-form>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
export default {
  name: "register",
  data() {
    const validateNumber = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入用户名"));
      }
    };
    const validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else if (value.length !== 6) {
        callback(new Error("密码长度须为6"));
      } else {
        if (this.form.password2 !== "") {
          this.$refs.form.validateField("password2");
        }
      }
    };
    const validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.form.password) {
        callback(new Error("两次密码不匹配"));
      }
    };
    return {
      form: {
        number: "",
        password: "",
        password2: "",
        userType: "1"
      },
      rules: {
        number: [{ required: true, validator: validateNumber, trigger: "blur" }],
        password: [{ required: true, validator: validatePass, trigger: "blur" }],
        password2: [
          { required: true, validator: validatePass, trigger: "blur" }
        ],
        userType: [
          { required: true, message: "请选择用户类型", trigger: "change" }
        ]
      },

      // activeName: "用户登录界面",
      // checked1: "",
      // checked2: '',
      // form3: {},
      userList: []
      // show_login: true
    };
  },
  methods: {
    register(formName) {
      alert("注册成功！");
      this.$router.push("/login");
      this.axios({
        url: "http://localhost:8080/sign/signIn",
        method: "post", //方式
        data: {
          number: this.form.number,
          password: this.form.password,
          password2: this.form.password2,
          userType: this.form.userType
        }
      }).then(res => {
        let resp = res.data;
        if (resp.code == 0) {
          this.$message({
            message: resp.message,
            type: "success"
          });
        } else {
          this.$message({
            message: resp.message,
            type: "warning"
          });
        }
      });
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style scoped>
.login-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  background-image: url("../assets/login-background.jpg");
  background-size: 100% 100%;
}

.form-title {
  width: 100%;
  line-height: 50px;
  text-align: center;
  font-size: 20px;
  color: #fff;
  border-bottom: 1px solid #ddd;
}

.login-table {
}
.login-form {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 350px;
  margin: -250px 0 0 -175px;
  border-radius: 5px;
  background: rgba(0, 0, 0, 0.6);
  overflow: hidden;
}

.form-content {
  padding: 30px 30px;
}

.login-btn {
  text-align: center;
}

.login-btn button {
  width: 100%;
  height: 36px;
}
.el-radio {
  color: #fff;
}
</style>
